#include "sceneCamera.h"


sceneCamera::sceneCamera() {
	init();
}

void sceneCamera::init() {
	angle = 3.141592, angleY = 0.0, deltaAngle = 0.0, deltaAngleY = 0.0;
	rotation = false;
	deltaMove = 0.0; deltaStrafe = 0.0;
	X = 0.0f; Y = 0.0f;

	cameraPos.x = 0.0f; cameraPos.y = 0.0f; cameraPos.z = -20.0f;
	cameraTargetVect.x = sin(angle);
	cameraTargetVect.y = angleY;
    cameraTargetVect.z = -cos(angle);
	cameraUp.x = 0.0f; cameraUp.y = 1.0f; cameraUp.z = 0.0f;
}

void sceneCamera::orientView(float ang) {
    cameraTargetVect.x = sin(ang);
    cameraTargetVect.z = -cos(ang);
}

void sceneCamera::orientViewY (float ang) {
    cameraTargetVect.y = ang;
}

void sceneCamera::move(float i) {
    cameraPos.x += i*(cameraTargetVect.x)*0.1f;
	cameraPos.y += i*(cameraTargetVect.y)*0.1f;
    cameraPos.z += i*(cameraTargetVect.z)*0.1f;
}

void sceneCamera::strafe(float i) {
	D3DXVECTOR3 strafeVector;
	D3DXVec3Cross(&strafeVector,&cameraTargetVect,&cameraUp);
	
	cameraPos.x += i*(strafeVector.x)*0.1f;
	cameraPos.y += i*(strafeVector.y)*0.1f;
    cameraPos.z += i*(strafeVector.z)*0.1f;
}

void sceneCamera::camStartRot(float amount)
{
	rotation = true;
    deltaAngle = amount;
}

void sceneCamera::camStopRot()
{
	rotation = false;
}

void sceneCamera::camStartMov(float amount)
{
	deltaMove = amount;
}

void sceneCamera::camStopMov()
{
	deltaMove = 0;
}

void sceneCamera::camStartStrafe(float amount)
{
	deltaStrafe = amount;
}

void sceneCamera::camStopStrafe()
{
	deltaStrafe = 0;
}

void sceneCamera::mouseStart(float x, float y)
{
	X=x;
    Y=y;
}

void sceneCamera::mouseStop()
{
	deltaAngle  = 0.0f;
	deltaAngleY = 0.0f;
}

void sceneCamera::mouseMove(float x, float y) {
	deltaAngle = 0.01f * (X-x);
    deltaAngleY =  0.01f * (Y-y);

	X=x;
    Y=y;

    orientView(angle);
    orientViewY(angleY);
}


D3DXVECTOR3 sceneCamera::getPos()
{
	return cameraPos;
}

D3DXVECTOR3 sceneCamera::getTargetVect()
{
	return cameraTargetVect;
}

D3DXVECTOR3 sceneCamera::getUp()
{
	return cameraUp;
}

void sceneCamera::render()
{
	if (deltaMove)
        move(deltaMove);
	if (deltaStrafe)
		strafe(deltaStrafe);
    if (deltaAngle || deltaAngleY) {
        angle += deltaAngle;
        angleY += deltaAngleY;
        orientView(angle);
        orientViewY(angleY);
        if (!rotation) deltaAngle = 0;
        deltaAngleY = 0;
    }
}